function reallocExternEventObjectSet(p,n)
  use derived,only:ObjectSet
  implicit none

  type(ObjectSet),pointer,dimension(:) :: &
       reallocExternEventObjectSet,p
  integer,intent(in) :: n

  integer :: i,j,k
  integer :: nobj,ierr

  allocate(reallocExternEventObjectSet(1:n),stat=ierr)

  if (ierr.ne.0) call abnormal_stop(&
       "reallocExternEventObjectSet",&
       "Memory allocation causes an error.")

  do i=1,n
     nullify(reallocExternEventObjectSet(i)%obj)
  end do

  if (.not.associated(p)) return

  do i=1,n-1
     nobj=p(i)%n_obj
     reallocExternEventObjectSet(i)%n_obj=nobj
     reallocExternEventObjectSet(i)%pka_e=p(i)%pka_e
     allocate(reallocExternEventObjectSet(i)%obj(nobj))
     do j=1,nobj
        reallocExternEventObjectSet(i)%obj(j)%obj_type=&
             p(i)%obj(j)%obj_type
        do k=1,3
           reallocExternEventObjectSet(i)%obj(j)%pos(k)=&
                p(i)%obj(j)%pos(k)
        end do
        do k=1,3
           reallocExternEventObjectSet(i)%obj(j)%vec(k)=&
                p(i)%obj(j)%vec(k)
        end do
        reallocExternEventObjectSet(i)%obj(j)%dir=p(i)%obj(j)%dir
     end do
     deallocate(p(i)%obj)
  end do

  deallocate(p)

  return

end function reallocExternEventObjectSet
